package com.supermarket.business.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.supermarket.business.domain.SupermarketProduct;
import com.supermarket.business.mapper.SupermarketProductMapper;
import com.supermarket.business.mapper.SupermarketSalesDetailMapper;
import com.supermarket.common.utils.DateUtils;
import com.supermarket.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Objects;

import com.supermarket.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.supermarket.business.domain.SupermarketSalesDetail;
import com.supermarket.business.mapper.SupermarketSalesOrderMapper;
import com.supermarket.business.domain.SupermarketSalesOrder;
import com.supermarket.business.service.ISupermarketSalesOrderService;

import javax.annotation.Resource;

/**
 * 销售订单Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-23
 */
@Service
public class SupermarketSalesOrderServiceImpl implements ISupermarketSalesOrderService
{
    @Resource
    private SupermarketSalesOrderMapper supermarketSalesOrderMapper;
    @Resource
    private SupermarketProductMapper supermarketProductMapper;
    @Resource
    private SupermarketSalesDetailMapper supermarketSalesDetailMapper;

    /**
     * 查询销售订单
     *
     * @param orderId 销售订单主键
     * @return 销售订单
     */
    @Override
    public SupermarketSalesOrder selectSupermarketSalesOrderByOrderId(Long orderId)
    {
        return supermarketSalesOrderMapper.selectSupermarketSalesOrderByOrderId(orderId);
    }

    /**
     * 查询销售订单列表
     *
     * @param supermarketSalesOrder 销售订单
     * @return 销售订单
     */
    @Override
    public List<SupermarketSalesOrder> selectSupermarketSalesOrderList(SupermarketSalesOrder supermarketSalesOrder)
    {
        return supermarketSalesOrderMapper.selectSupermarketSalesOrderList(supermarketSalesOrder);
    }

    /**
     * 新增销售订单
     *
     * @param supermarketSalesOrder 销售订单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSupermarketSalesOrder(SupermarketSalesOrder supermarketSalesOrder)
    {
        List<SupermarketSalesDetail> supermarketSalesDetailList = supermarketSalesOrder.getSupermarketSalesDetailList();
        initSupermarketSalesDetailList(supermarketSalesDetailList);
        //校验每个订单明细的商品库存是否足够
        updateProductStock(supermarketSalesDetailList);
        //计算订单总金额
        BigDecimal totalAmount = BigDecimal.ZERO;
        for (SupermarketSalesDetail supermarketSalesDetail : supermarketSalesDetailList){
           totalAmount = totalAmount.add(supermarketSalesDetail.getAmount());
        }
        supermarketSalesOrder.setTotalAmount(totalAmount);
        int rows = supermarketSalesOrderMapper.insertSupermarketSalesOrder(supermarketSalesOrder);
        insertSupermarketSalesDetail(supermarketSalesOrder);
        return rows;
    }

    /**
     * 修改销售订单
     *
     * @param supermarketSalesOrder 销售订单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSupermarketSalesOrder(SupermarketSalesOrder supermarketSalesOrder)
    {
        List<SupermarketSalesDetail> supermarketSalesDetailList = supermarketSalesOrder.getSupermarketSalesDetailList();
        initSupermarketSalesDetailList(supermarketSalesDetailList);
        //校验并更新每个订单明细的商品库存
        updateProductStock(supermarketSalesDetailList);
        //计算订单总金额
        BigDecimal totalAmount = BigDecimal.ZERO;
        for (SupermarketSalesDetail supermarketSalesDetail : supermarketSalesDetailList){
            totalAmount = totalAmount.add(supermarketSalesDetail.getAmount());
        }
        supermarketSalesOrder.setTotalAmount(totalAmount);
        supermarketSalesOrderMapper.deleteSupermarketSalesDetailByOrderId(supermarketSalesOrder.getOrderId());
        insertSupermarketSalesDetail(supermarketSalesOrder);
        return supermarketSalesOrderMapper.updateSupermarketSalesOrder(supermarketSalesOrder);
    }

    /**
     * 批量删除销售订单
     *
     * @param orderIds 需要删除的销售订单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSupermarketSalesOrderByOrderIds(Long[] orderIds)
    {
        supermarketSalesOrderMapper.deleteSupermarketSalesDetailByOrderIds(orderIds);
        return supermarketSalesOrderMapper.deleteSupermarketSalesOrderByOrderIds(orderIds);
    }

    /**
     * 删除销售订单信息
     *
     * @param orderId 销售订单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSupermarketSalesOrderByOrderId(Long orderId)
    {
        supermarketSalesOrderMapper.deleteSupermarketSalesDetailByOrderId(orderId);
        return supermarketSalesOrderMapper.deleteSupermarketSalesOrderByOrderId(orderId);
    }

    /**
     * 新增销售明细信息
     *
     * @param supermarketSalesOrder 销售订单对象
     */
    public void insertSupermarketSalesDetail(SupermarketSalesOrder supermarketSalesOrder)
    {
        List<SupermarketSalesDetail> supermarketSalesDetailList = supermarketSalesOrder.getSupermarketSalesDetailList();
        Long orderId = supermarketSalesOrder.getOrderId();
        if (StringUtils.isNotNull(supermarketSalesDetailList))
        {
            List<SupermarketSalesDetail> list = new ArrayList<SupermarketSalesDetail>();
            for (SupermarketSalesDetail supermarketSalesDetail : supermarketSalesDetailList)
            {
                supermarketSalesDetail.setOrderId(orderId);
                list.add(supermarketSalesDetail);
            }
            if (list.size() > 0)
            {
                for (SupermarketSalesDetail supermarketSalesDetail : list){
                    supermarketSalesDetail.setCreateTime(DateUtils.getNowDate());
                    supermarketSalesDetail.setUpdateTime(DateUtils.getNowDate());
                    supermarketSalesDetail.setCreateId(SecurityUtils.getUserId());
                    supermarketSalesDetail.setUpdateId(SecurityUtils.getUserId());
                    supermarketSalesDetailMapper.insertSysSalesDetail(supermarketSalesDetail);
                }
            }
        }

    }

    /**
     * 校验并更新每个订单明细的商品库存
     */
    @Transactional
    public void updateProductStock(List<SupermarketSalesDetail> supermarketSalesDetailList){
        for (SupermarketSalesDetail supermarketSalesDetail : supermarketSalesDetailList){
            //回溯之前的商品库存
            SupermarketSalesDetail supermarketSalesDetail1 = supermarketSalesDetailMapper.selectSysSalesDetailByDetailId(supermarketSalesDetail.getDetailId());
            if(Objects.nonNull(supermarketSalesDetail1)){
                SupermarketProduct product1 = supermarketProductMapper.selectSysProductByProductId(supermarketSalesDetail1.getProductId());
                product1.setStock(product1.getStock() + supermarketSalesDetail1.getQuantity());
                supermarketProductMapper.updateSysProduct(product1);
            }
            //更新当前商品库存
            SupermarketProduct product = supermarketProductMapper.selectSysProductByProductId(supermarketSalesDetail.getProductId());
            if(product.getStock()<supermarketSalesDetail.getQuantity()){
                throw new IllegalArgumentException("商品"+product.getProductName()+"库存不足");
            }
            product.setStock(product.getStock() - supermarketSalesDetail.getQuantity());
            supermarketProductMapper.updateSysProduct(product);
        }
    }

    public void initSupermarketSalesDetailList(List<SupermarketSalesDetail> supermarketSalesDetailList){
        for (SupermarketSalesDetail supermarketSalesDetail : supermarketSalesDetailList){
            if(supermarketSalesDetail.getProductId() == null){
                Long productId = Long.valueOf(supermarketSalesDetail.getProductName());
                SupermarketProduct product = supermarketProductMapper.selectSysProductByProductId(productId);
                supermarketSalesDetail.setProductName(product.getProductName());
                supermarketSalesDetail.setProductId(product.getProductId());
            }
            SupermarketProduct product1 = supermarketProductMapper.selectSysProductByProductId(supermarketSalesDetail.getProductId());
            if(!Objects.equals(product1.getProductName(),supermarketSalesDetail.getProductName())){
                SupermarketProduct product = supermarketProductMapper.selectSysProductByProductId(Long.valueOf(supermarketSalesDetail.getProductName()));
                supermarketSalesDetail.setProductName(product.getProductName());
                supermarketSalesDetail.setProductId(product.getProductId());
            }

            SupermarketProduct product = supermarketProductMapper.selectSysProductByProductId(supermarketSalesDetail.getProductId());
            supermarketSalesDetail.setUnitPrice(product.getPrice());
            Long quantity = supermarketSalesDetail.getQuantity();
            BigDecimal unitPrice = supermarketSalesDetail.getUnitPrice();
            BigDecimal amount = unitPrice.multiply(BigDecimal.valueOf(quantity));
            supermarketSalesDetail.setAmount(amount);
        }
    }


}
